﻿using System;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.Events;

namespace Rio.Framework.Pages.CriteriaControls
{
    /// <summary>
    /// Base class of CriteriaControl
    /// </summary>
    public abstract class CriteriaControlBase : ControlBase
    {

        #region Attributs
        
        private Guid _uniqueId;
        protected Brush _originalBorderBrush;

        #endregion

        #region Events

        public delegate void RemoveClickHandler(object sender, EventArgs e);
        public event RemoveClickHandler RemoveControlClick;

        /// <summary>
        /// Event raised when remove control button is clicked
        /// </summary>
        /// <param name="e"></param>
        protected void OnRemoveControlClick(EventArgs e)
        {
            if (RemoveControlClick != null)
            {
                RemoveControlClick(this, e);
            }
        }

        public delegate void FilterClickHandler(object sender, FilterClickEventArgs e);
        public event FilterClickHandler FilterClick;

        /// <summary>
        /// Event raised when filter button is clicked
        /// </summary>
        /// <param name="e"></param>
        protected void OnFilterClick(FilterClickEventArgs e)
        {
            if (FilterClick != null)
            {
                FilterClick(this, e);
            }
        }

        ///<summary>
        ///Define an Event based on the ValueHandler Delegate
        ///</summary>
        public event ValueHandler ValueValidated;
        public delegate void ValueHandler(object sender, EventArgs e);

        ///<summary>
        ///Invoke the value validated event; called whenever a value is svalidated
        ///</summary>
        ///<param name="e"></param>        
        protected void OnValueValidated(EventArgs e)
        {
            if (ValueValidated != null)
            {
                ValueValidated(this, e);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Get the unique identifier of the control
        /// </summary>
        public Guid UniqueId
        {
            get { return _uniqueId; }
        }

        /// <summary>
        /// Accept SpecialChars in regex
        /// </summary>
        public bool AcceptSpecialCharsForRegularExpression { get; set; }

        /// <summary>
        /// Get or set Index capability
        /// </summary>
        public Index CurrentIndex { get; set; }

        /// <summary>
        /// Get or set the control value
        /// </summary>
        public abstract string InputValue { get; set; }

        /// <summary>
        /// Gets or sets the input search value.
        /// </summary>
        /// <value>
        /// The input search value.
        /// </value>
        public virtual string InputSearchValue
        {
            get
            {
                return InputValue;
            }
            set
            {
                InputValue = value;
            }
        }

        /// <summary>
        /// IsDocumentSearchIndex
        /// </summary>
        public bool IsDocumentSearchIndex { get; set; }

        /// <summary>
        /// Gets or sets the selected value after init.
        /// </summary>
        /// <value>The selected value after init.</value>
        public String SelectedValueAfterInit { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CriteriaControlBase"/> class.
        /// </summary>
        protected CriteriaControlBase()
        {
            _uniqueId = Guid.NewGuid();
            _originalBorderBrush = BorderBrush as LinearGradientBrush;
        }

        #endregion

        #region Private Functions

        #endregion

        #region Public Functions

        /// <summary>
        /// Used to set the default index value
        /// </summary>
        public virtual void SetDefaultValue()
        {
            if (!string.IsNullOrEmpty(this.CurrentIndex.DefaultValue))
            {
                this.InputValue = this.CurrentIndex.DefaultValue;
            }
        }

        /// <summary>
        /// Sets the index by text.
        /// </summary>
        /// <param name="selectValueAsText">The P_L select value as text.</param>
        public virtual void SetIndexByText(string selectValueAsText)
        {
            InputValue = selectValueAsText;
        }

        /// <summary>
        /// Used to clear value
        /// </summary>
        public virtual void Clear()
        {
            this.InputValue = string.Empty;
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public virtual void ChangeBorderColor(Color color)
        {
            this.BorderBrush = new SolidColorBrush(color);
        }

        /// <summary>
        /// Reverts the color of the border.
        /// </summary>
        public virtual void RevertBorderColor()
        {
            this.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Must be override in child class
        /// </summary>
        public override void InitLabel()
        {
        }

        /// <summary>
        /// Set focus on the control
        /// </summary>
        public void SetFocus()
        {
            this.Focus();
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public abstract void SetMaxLength();

        /// <summary>
        /// Hides Mandatory label 
        /// </summary>
        public abstract void DisableMandatoryLabel();

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public abstract void DisableIndexLabel();

        /// <summary>
        /// Hides filter button
        /// </summary>
        public abstract void DisableFilter();

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public abstract void ActiveRemove();

        #endregion

    }
}
